package com.aaa.demo10;

import java.util.concurrent.*;

/**
 * @author ${崔灿}
 * @date 2022/11/02 19:14
 **/

public class TestExecutors {
    public static void main(String[] args) {
        //创建单一线程池---池子中有且仅有一个线程对象
        //ExecutorService executorService = Executors.newSingleThreadExecutor();

        //创建固定长度的线程池---池子中线程的数量是固定的。---里面预先会创建n个线程对象
        //ExecutorService executorService = Executors.newFixedThreadPool(5);

        //创建可变长度的线程池---根据任务个数动态创建线程对象。
        //ExecutorService executorService = Executors.newCachedThreadPool();

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
        //往线程池提交任务
        for (int i = 0; i < 5; i++) {
            final int a=i;
           executorService.schedule(new Runnable() {
               @Override
               public void run() {
                   System.out.println(Thread.currentThread().getName()+"***"+a);
               }
           } , 20 , TimeUnit.SECONDS);
           executorService.submit(new Runnable() {

               @Override
               public void run() {
                   System.out.println(Thread.currentThread().getName()+"***"+a);
               }
           });
        }
    }
}
